package com.example.comiccenter.service.implt;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.comiccenter.dao.ComicMapper;
import com.example.comiccenter.dao.ScoreMapper;
import com.example.comiccenter.domain.Comic;
import com.example.comiccenter.domain.Score;
import com.example.comiccenter.service.ScoreService;
import com.example.comiccenter.utils.ResultData;
import com.example.comiccenter.utils.SearchBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class ScoreServiceImpl implements ScoreService {

    @Resource
    private ScoreMapper scoreMapper;
    @Resource
    private ComicMapper comicMapper;

    @Override
    public ResultData getAllScore(SearchBean<Score> searchBean) {

        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        wrapper.eq(searchBean.getData().getUsername()!=null,"username",searchBean.getData().getUsername())
                .eq(searchBean.getData().getComicId()!=null,"comicId",searchBean.getData().getComicId());
        Page<Score> scorePage = scoreMapper.selectPage(new Page<>(searchBean.getPageNum(), searchBean.getPageSize()), wrapper);
        return ResultData.success("查询成功",scorePage);
    }

    @Override
    public ResultData addScore(Score score) {
        if (score.getUsername() == null || score.getComicId() == null){
            return ResultData.error(400,"传入参数错误");
        }
        if (score.getScore()<0 || score.getScore()>5){
            return ResultData.error(400,"评分必须为1-5之间的整数");
        }
        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        wrapper.eq("username",score.getUsername()).eq("comicId",score.getComicId());
        if (scoreMapper.selectCount(wrapper)>0) {
            return ResultData.success("该作品已评分");
        }
        scoreMapper.insert(score);

        //添加评分完后更新comic的平均评分
        QueryWrapper<Comic> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("id",score.getComicId());
        Comic comic = comicMapper.selectOne(wrapper1);
        float v = (comic.getScore() * (float) comic.getScoreNumber() +(float) score.getScore())/((float) comic.getScoreNumber()+1);
        comic.setScoreNumber(comic.getScoreNumber()+1);
        comic.setScore(v);
        comicMapper.updateById(comic);

        return ResultData.success("评分成功");
    }

    @Override
    public ResultData deleteScore(Score score) {
        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        wrapper.eq("username",score.getUsername()).eq("comicId",score.getComicId());
        score = scoreMapper.selectOne(wrapper);
        //获取评分
        Integer score2 = score.getScore();


        scoreMapper.delete(wrapper);

        //更新comic当前评分
        QueryWrapper<Comic> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("id",score.getComicId());
        Comic comic = comicMapper.selectOne(wrapper1);
        Integer scoreNumber = comic.getScoreNumber();
        float v = (comic.getScore() * (float) scoreNumber - (float) score.getScore()) / (scoreNumber - 1);
        comic.setScore(v);
        comic.setScoreNumber(scoreNumber-1);
        comicMapper.updateById(comic);

        return ResultData.success("取消评分成功");
    }

}
